const inquirer = require('inquirer');
const path = require('path');
const fs = require('fs-extra');

const VUE_KEY = 'Vue';
const REACT_KEY = 'React';

function createReactApp({ name }) {
  const projectName = name;
  console.info('create react');
  fs.copySync(
    path.resolve(__dirname, "../templates/react"),
    path.resolve(process.cwd(), projectName),
    {
      overwrite: false,
      errorOnExist: true,
      filter: (src) => {
        console.info(src);
        if (src.includes("node_modules")) {
          return false;
        }
        return true;
      },
    }
  );
}

function createVueApp({ name }) {
  const projectName = name;
  console.info('create vue');
  fs.copySync(
    path.resolve(__dirname, "../templates/Vue"),
    path.resolve(process.cwd(), projectName),
    {
      overwrite: false,
      errorOnExist: true,
      filter: (src) => {
        console.info(src);
        if (src.includes("node_modules")) {
          return false;
        }
        return true;
      },
    }
  );
}

const creatorMap = {
  [VUE_KEY]: createVueApp,
  [REACT_KEY]: createReactApp,
}

function creator(options) {
  const prompt = inquirer.createPromptModule();
  const questions = [
    {
      type: 'list',
      name: 'framework',
      message: '请选择框架',
      choices: [REACT_KEY, VUE_KEY],
      default: 'React',
    }
  ]
  prompt(questions).then((res) => {
    const createFn = creatorMap[res.framework];
    createFn(options);
  });
}

module.exports  = creator;
